package com.lottery.ruok.lottrey.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.bean.LotteryItem;
import com.lottery.ruok.lottrey.dialog.TraditionBuyDialog;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ASUS on 2017/11/13.
 */

public class TraditionBuyAdapter extends BaseAdapter {
    private ArrayList<LotteryItem> lotterys;
    private TraditionBuyDialog traditionBuyDialog;

    public TraditionBuyAdapter(ArrayList<LotteryItem> lotterys, TraditionBuyDialog traditionBuyDialog){
        this.lotterys = lotterys;
        this.traditionBuyDialog = traditionBuyDialog;
    }

    @Override
    public int getCount() {
        return lotterys.size();
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(traditionBuyDialog.getActivity()).inflate(R.layout.tradition_buy_item, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        holder.name.setText(lotterys.get(i).title + "-" + lotterys.get(i).name);

        holder.money.setText(lotterys.get(i).fillInString + "元");

        holder.no.setText("第" + traditionBuyDialog.getArguments().getString("currentNo") + "期");
        holder.odd.setText(lotterys.get(i).odd);
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(lotterys.size() > 0) {
                    lotterys.remove(i);

                    traditionBuyDialog.setMoney();
                }
            }
        });

        return view;
    }

    static class ViewHolder {
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.money)
        TextView money;
        @BindView(R.id.odd)
        TextView odd;
        @BindView(R.id.no)
        TextView no;
        @BindView(R.id.delete)
        ImageView delete;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
